################################################
## Replication Code for Bayesian Hierarchical Models in     ##
## "War and the Re-election Motive: Examining ##
## the Effect of Term Limits"                ##
################################################


setwd("~/Dropbox/Filesystem/Latex/Term Limits/Term Limits (1)/FinalSubmission/Erratum/")

# load necessary libraries
library(foreign)
library(MCMCglmm)
library(xtable)

#####################
## Table 2
#####################
# load data

term <- read.dta("termlimitinititation.dta")

term <- na.omit(term)
# fixes random numbers, otherwise results will differ slightly
set.seed(2)

glmm1 <- MCMCglmm(cwinit~distance+polity2receiver+relcap+majpow1+majpow2+tl2,random=~id,data=term,family="categorical",thin=10,burnin=2000,nitt=20000,verbose=FALSE,pr=TRUE,pl=TRUE,saveX=TRUE,saveZ=TRUE)
summary(glmm1)


a <- posterior.mode(glmm1$Sol)
b <- HPDinterval(glmm1$Sol,0.95)
tab2 <- cbind(a[1:12],b[1:12,])

table2 <- xtable(tab2,digits=c(0,3,3,3))
print(table2)

# interaction with recession
glmm2 <- MCMCglmm(cwinit~distance+polity2receiver+relcap+majpow1+majpow2+tl2+rec4+tl2:rec4,random=~id,data=test2,family=   "categorical",thin=10,burnin=2000,nitt=20000,verbose=FALSE,pr=TRUE,pl=TRUE,saveX=TRUE,saveZ=TRUE)
summary(glmm2)

a <- posterior.mode(glmm2$Sol)
b <- HPDinterval(glmm2$Sol,0.95)
tab2 <- cbind(a[1:12],b[1:12,])

table2 <- xtable(tab2,digits=c(0,3,3,3))
print(table2)





##################################
### Conflict Targeting
##################################

term2 <- read.dta("termlimittarget.dta")

term2 <- na.omit(term2)

# fixes random numbers, otherwise results will differ slightly
set.seed(4)

glmm3 <- MCMCglmm(cwinit~distance+polity2sender+relcap+majpow1+majpow2+tl2,random=~id,data=term2,family="categorical",thin=10,burnin=2000,nitt=20000,verbose=FALSE,pr=TRUE,pl=TRUE,saveX=TRUE,saveZ=TRUE)
summary(glmm3)


a <- posterior.mode(glmm3$Sol)
b <- HPDinterval(glmm3$Sol,0.95)
tab2 <- cbind(a[1:12],b[1:12,])

table2 <- xtable(tab2,digits=c(0,3,3,3))
print(table2)





